<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xiangqin:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['xiangqin:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xiangqin:member:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xiangqin:member:export']"
        >导出</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="el-form-search">
        <el-form-item label="年龄" prop="age" class="el-form-search-item">
          <el-input
            v-model="queryParams.age"
            placeholder="请输入年龄"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="生肖" prop="animals" class="el-form-search-item">
          <el-input
            v-model="queryParams.animals"
            placeholder="请输入生肖"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="户籍" prop="domicile" class="el-form-search-item">
          <el-input
            v-model="queryParams.domicile"
            placeholder="请输入户籍"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="居住地" prop="habitation" class="el-form-search-item">
          <el-input
            v-model="queryParams.habitation"
            placeholder="请输入居住地"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="身高" prop="height" class="el-form-search-item">
          <el-input
            v-model="queryParams.height"
            placeholder="请输入身高"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="学历" prop="education" class="el-form-search-item">
          <el-input
            v-model="queryParams.education"
            placeholder="请输入学历"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="性格标签" prop="characters" class="el-form-search-item">
          <el-input
            v-model="queryParams.characters"
            placeholder="请输入性格标签"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>


<!--        <el-form-item label="年薪" prop="salaryStart" class="el-form-search-item">-->
<!--          <el-input-->
<!--            v-model="queryParams.salaryStart"-->
<!--            placeholder="请输入年薪"-->
<!--            clearable-->
<!--            size="mini"-->
<!--            @keyup.enter.native="handleQuery"-->
<!--          />-->
<!--        </el-form-item>-->


        <el-form-item label="所在公司" prop="company" class="el-form-search-item">
          <el-input
            v-model="queryParams.company"
            placeholder="请输入所在公司"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="职业" prop="career" class="el-form-search-item">
          <el-input
            v-model="queryParams.career"
            placeholder="请输入职业"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="购车" prop="car" class="el-form-search-item">
          <el-input
            v-model="queryParams.car"
            placeholder="请输入购车"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="家庭" prop="family" class="el-form-search-item">
          <el-input
            v-model="queryParams.family"
            placeholder="请输入家庭"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="购房" prop="house" class="el-form-search-item">
          <el-input
            v-model="queryParams.house"
            placeholder="请输入购房"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
<!--        <el-form-item label="择偶需求" prop="demand" class="el-form-search-item">-->
<!--          <el-input-->
<!--            v-model="queryParams.demand"-->
<!--            placeholder="请输入择偶需求"-->
<!--            clearable-->
<!--            size="mini"-->
<!--            @keyup.enter.native="handleQuery"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item label="是否已婚" prop="isMarriage" class="el-form-search-item">
          <el-input
            v-model="queryParams.isMarriage"
            placeholder="请输入是否已婚"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="el-form-search-item">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" type="index" width="50" align="center">
        <template scope="scope">
          <span>{{
              (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="生肖" align="center" prop="animals" />
      <el-table-column label="户籍" align="center" prop="domicile" />
      <el-table-column label="居住地" align="center" prop="habitation" />
      <el-table-column label="身高" align="center" prop="height" />

      <el-table-column label="会员照片url" align="center" prop="memberUrl" >
        <template scope="scope">
          <image-preview :src="scope.row.memberUrl" :width="30" :height="30">
          </image-preview>
        </template>

      </el-table-column>

      <el-table-column label="学历" align="center" prop="education" />
      <el-table-column label="性格标签" align="center" prop="characters" />
      <el-table-column label="年薪开始区间" align="center" prop="salaryStart" />
      <el-table-column label="年薪结束区间" align="center" prop="salaryEnd" />
      <el-table-column label="所在公司" align="center" prop="company" />
      <el-table-column label="职业" align="center" prop="career" />
      <el-table-column label="购车" align="center" prop="car" />
      <el-table-column label="家庭" align="center" prop="family" />
      <el-table-column label="购房" align="center" prop="house" />
      <el-table-column label="择偶需求" align="center" prop="demand" />

      <el-table-column label="是否已婚" align="center" prop="isMarriage">
        <template scope="scope">
          <span> {{scope.row.isMarriage==0?'未婚':'已婚'}}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xiangqin:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xiangqin:member:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="生肖" prop="animals">
          <el-input v-model="form.animals" placeholder="请输入生肖" />
        </el-form-item>
        <el-form-item label="户籍" prop="domicile">
          <el-input v-model="form.domicile" placeholder="请输入户籍" />
        </el-form-item>
        <el-form-item label="居住地" prop="habitation">
          <el-input v-model="form.habitation" placeholder="请输入居住地" />
        </el-form-item>
        <el-form-item label="身高" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="会员照片url" prop="memberUrl">
<!--          <el-input v-model="form.memberUrl" placeholder="请输入会员照片url" />-->
          <template scope="scope">
            <qiniu-image-upload v-model="form.memberUrl">
            </qiniu-image-upload>
          </template>

        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="性格标签" prop="characters">
          <el-input v-model="form.characters" placeholder="请输入性格标签" />
        </el-form-item>
        <el-form-item label="年薪开始区间" prop="salaryStart">
          <el-input v-model="form.salaryStart" placeholder="请输入年薪开始区间" />
        </el-form-item>
        <el-form-item label="年薪结束区间" prop="salaryEnd">
          <el-input v-model="form.salaryEnd" placeholder="请输入年薪结束区间" />
        </el-form-item>
        <el-form-item label="所在公司" prop="company">
          <el-input v-model="form.company" placeholder="请输入所在公司" />
        </el-form-item>
        <el-form-item label="工作性质" prop="workType">
          <el-input v-model="form.workType" placeholder="请输入工作性质" />
        </el-form-item>
        <el-form-item label="职业" prop="career">
          <el-input v-model="form.career" placeholder="请输入职业" />
        </el-form-item>
        <el-form-item label="购车" prop="car">
          <el-input v-model="form.car" placeholder="请输入购车" />
        </el-form-item>
        <el-form-item label="家庭" prop="family">
          <el-input v-model="form.family" placeholder="请输入家庭" />
        </el-form-item>
        <el-form-item label="购房" prop="house">
          <el-input v-model="form.house" placeholder="请输入购房" />
        </el-form-item>
        <el-form-item label="择偶需求" prop="demand">
          <el-input v-model="form.demand" placeholder="请输入择偶需求" />
        </el-form-item>
        <el-form-item label="是否已婚" prop="isMarriage">
          <el-input v-model="form.isMarriage" placeholder="请输入是否已婚" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember } from "@/api/xiangqin/member";

export default {
  name: "Member",
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员信息表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "create_time",
        isAsc: "desc",
        age: null,
        animals: null,
        domicile: null,
        habitation: null,
        height: null,
        memberUrl: null,
        education: null,
        characters: null,
        salaryStart: null,
        salaryEnd: null,
        company: null,
        career: null,
        car: null,
        family: null,
        house: null,
        demand: null,
        isMarriage: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }
        ],
        animals: [
          { required: true, message: "生肖不能为空", trigger: "blur" }
        ],
        domicile: [
          { required: true, message: "户籍不能为空", trigger: "blur" }
        ],
        habitation: [
          { required: true, message: "居住地不能为空", trigger: "blur" }
        ],
        height: [
          { required: true, message: "身高不能为空", trigger: "blur" }
        ],
        memberUrl: [
          { required: true, message: "会员照片url不能为空", trigger: "blur" }
        ],
        education: [
          { required: true, message: "学历不能为空", trigger: "blur" }
        ],
        characters: [
          { required: true, message: "性格标签不能为空", trigger: "blur" }
        ],
        salaryStart: [
          { required: true, message: "年薪开始区间不能为空", trigger: "blur" }
        ],
        salaryEnd: [
          { required: true, message: "年薪结束区间不能为空", trigger: "blur" }
        ],
        company: [
          { required: true, message: "所在公司不能为空", trigger: "blur" }
        ],
        career: [
          { required: true, message: "职业不能为空", trigger: "blur" }
        ],
        car: [
          { required: true, message: "购车不能为空", trigger: "blur" }
        ],
        family: [
          { required: true, message: "家庭不能为空", trigger: "blur" }
        ],
        house: [
          { required: true, message: "购房不能为空", trigger: "blur" }
        ],
        demand: [
          { required: true, message: "择偶需求不能为空", trigger: "blur" }
        ],
        isMarriage: [
          { required: true, message: "是否已婚不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员信息列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        age: null,
        animals: null,
        domicile: null,
        habitation: null,
        height: null,
        memberUrl: null,
        education: null,
        characters: null,
        salaryStart: null,
        salaryEnd: null,
        company: null,
        workType: null,
        career: null,
        car: null,
        family: null,
        house: null,
        demand: null,
        isMarriage: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function() {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xiangqin/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
